package com.shujia.akka.clientAndServer

import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import com.typesafe.config.{Config, ConfigFactory}

import scala.io.StdIn

object Server extends App {
  val (serverHost, serverPort) = ("127.0.0.1", 8888)


  private val config: Config = ConfigFactory.parseString(
    s"""
       |akka.actor.provider="akka.remote.RemoteActorRefProvider"
       |akka.remote.netty.tcp.hostname=$serverHost
       |akka.remote.netty.tcp.port=$serverPort
       |""".stripMargin
  )

  private val tongTongSystem: ActorSystem = ActorSystem("tongTongServer", config)
  private val serverRef: ActorRef = tongTongSystem.actorOf(Props[Server], "server")

  serverRef ! "start"


}
class Server extends Actor {
  override def receive: Receive = {
    case "start" => {
      println("童童聊天室后台服务启动了..")
    }
    case ClientMessage(message:String) => {
      println("client:"+message)
      print("请输入回应消息：")
      val serverMessage: String = StdIn.readLine()
      sender() ! ServerMessage(serverMessage)
    }
  }
}
